package com.ruoyi.web.utils;

import com.ruoyi.kubernetes.models.RoleBindingRequestData;
import com.ruoyi.web.domain.KubernetesGroupRoleBinding;
import com.ruoyi.web.domain.KubernetesUserRoleBinding;

public class KubernetesUtils {

    public static RoleBindingRequestData getRoleBindingRequestData(KubernetesUserRoleBinding kubernetesUserRoleBinding){
        RoleBindingRequestData roleBindingRequestData = new RoleBindingRequestData();
        roleBindingRequestData.setGroupBinding(false);
        roleBindingRequestData.setClusterBinding(kubernetesUserRoleBinding.getClusterBinding()==1);
        roleBindingRequestData.setNamespace(kubernetesUserRoleBinding.getNamespace());
        roleBindingRequestData.setUsername(kubernetesUserRoleBinding.getUserName());
        roleBindingRequestData.setRoleName(kubernetesUserRoleBinding.getRoleName());
        String bindingName = "user-"+kubernetesUserRoleBinding.getUserName()+"-"+roleBindingRequestData.getRoleName()+"-";
        if (!roleBindingRequestData.isClusterBinding()){
            bindingName+=roleBindingRequestData.getNamespace()+"-";
        }
        bindingName+="binding";
        roleBindingRequestData.setName(bindingName);
        return roleBindingRequestData;
    }

    public static RoleBindingRequestData getRoleBindingRequestData(KubernetesGroupRoleBinding kubernetesGroupRoleBinding){
        RoleBindingRequestData roleBindingRequestData = new RoleBindingRequestData();
        roleBindingRequestData.setGroupBinding(true);
        roleBindingRequestData.setClusterBinding(kubernetesGroupRoleBinding.getClusterBinding()==1);
        roleBindingRequestData.setNamespace(kubernetesGroupRoleBinding.getNamespace());
        roleBindingRequestData.setGroupName(kubernetesGroupRoleBinding.getGroupName());
        roleBindingRequestData.setRoleName(kubernetesGroupRoleBinding.getRoleName());
        String bindingName = "group-"+kubernetesGroupRoleBinding.getGroupName()+"-"+roleBindingRequestData.getRoleName()+"-";
        if (!roleBindingRequestData.isClusterBinding()){
            bindingName+=roleBindingRequestData.getNamespace()+"-";
        }
        bindingName+="binding";
        roleBindingRequestData.setName(bindingName);
        return roleBindingRequestData;
    }
}
